package com.example.fiction.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.example.fiction.Constant;
import com.example.fiction.app.MyApplication;
import com.example.fiction.base.BaseResponse;
import com.example.fiction.exception.ResponseException;
import com.example.fiction.utils.ReadAssets;
import com.example.fiction.utils.TransferDataUtils;

import org.json.JSONException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * api参数请求
 * author: 姚鸿杰
 * time: 2019/4/19 23:06
 **/
public class ApiArgs {

    private static final String TAG = "ApiArgs";

    //传递参数
    public static Map<String, String> getApiArgs(String rawData, String method) throws IOException {
        Map<String, String> map = new HashMap<>();
        //todo 待添加token
        map.put("token", "hdsiuhfiu23uy98fh2398rhyf89ew");
        //获取AES密钥
        String key = TransferDataUtils.getAESPassword();
        //保存密钥
        SharedPreferences.Editor editor = MyApplication.getContext().
                getSharedPreferences("AesKey", Context.MODE_PRIVATE).edit();
        editor.putString("key", key);
        editor.apply();

        //数据用AES加密
        String data = TransferDataUtils.encrypterAES(rawData, key);
        //AES密钥用用RSA加密
        String sign = TransferDataUtils.encrypterPublicRSA(key);
        map.put("sign", sign);
        map.put("data", data);
        map.put("time", String.valueOf(new Date().getTime()));    //获取当前时间
        map.put("method", method);
        return map;
    }

    //接收数据
    public static String verifyArgs(BaseResponse response) throws ResponseException, IOException, JSONException {

        if (!response.isSuccess()) {    //访问服务器成功但是请求数据失败!
            throw new ResponseException(response.msg, response.code);
        }

        Log.i(TAG, "verifyArgs1: " + response.time);
        Log.i(TAG, "verifyArgs2: " + System.currentTimeMillis() / 1000);
        Log.i(TAG, "verifyArgs2: " + System.currentTimeMillis() );
        //计算请求时间差值
        long diff = new Date().getTime() / 1000 - Long.parseLong(response.time);
        Log.i(TAG, "diff: " + diff);
        if (diff > 600) {   //请求时间与当前时间超过10分钟认为该数据无效
            String[] result = ReadAssets.analysisTextException(Constant.SERVER_NAME, "timeOut");
            throw new ResponseException(result[1], Integer.parseInt(result[0]));
        }
        //获取AesKey密钥
        SharedPreferences sf = MyApplication.getContext().getSharedPreferences("AesKey", Context.MODE_PRIVATE);
        String aesKey = sf.getString("key", "");
        //解析数据
        //todo code未定义
        if (TextUtils.isEmpty(aesKey)) {
            String[] result = ReadAssets.analysisTextException(Constant.SERVER_NAME, "keyEmpty");
            throw new ResponseException(result[1], Integer.parseInt(result[0]));
        }
        String data = TransferDataUtils.decrypterAES(response.data, aesKey);
        boolean result = TransferDataUtils.verifyRSA(data, response.sign);
        if (result) return data;
        //todo 验签code未定义
        String[] fail = ReadAssets.analysisTextException(Constant.SERVER_NAME, "verifySign");
        throw new ResponseException(fail[1], Integer.parseInt(fail[0]));
    }
}
